<?php
/**
 * @package     PdoQuery.php
 * @author      Jing <tangjing3321@gmail.com>
 * @link        http://www.slimphp.net
 * @version     1.0
 * @copyright   Copyright (c) SlimCustom.
 * @date        2017年5月10日
 */

namespace SlimCustom\Libs\Model\Query;

/**
 * PdoQuery
 * 
 * @author Jing <tangjing3321@gmail.com>
 */
class PdoQuery
{
    /**
     * 表名
     * 
     * @var string
     */
    private $table;

    /**
     * 初始化
     * 
     * @param string $table
     */
    public function __construct($table = '')
    {
        ! $table ?: $this->table($table);
    }

    /**
     * 设置表名
     *
     * @param string $table            
     * @return \SlimCustom\Libs\Model\Query
     */
    public function table($table)
    {
        $this->table = $table;
        return $this;
    }

    /**
     * 查询
     * 
     * @param array $columns   
     * @return \Slim\PDO\Statement\SelectStatement         
     */
    public function select($columns = ['*'])
    {
        return database()->select($columns)->from($this->table);
    }

    /**
     * 插入
     * 
     * @param array $pairs            
     * @return \Slim\PDO\Statement\InsertStatement
     */
    public function insert($pairs = [])
    {
        return database()->insert($pairs)->into($this->table);
    }

    /**
     * 更新
     * 
     * @param array $pairs     
     * @return \Slim\PDO\Statement\UpdateStatement       
     */
    public function update($pairs = [])
    {
        return database()->update($pairs)->table($this->table);
    }

    /**
     * 删除
     * 
     * @param string $table            
     * @return \Slim\PDO\Statement\DeleteStatement
     */
    public function delete($table = null)
    {
        return database()->delete($table)->from($this->table);
    }
}